package com.edu.controller;


import com.R;
import com.edu.client.VodClient;
import com.edu.entity.Video;
import com.edu.exceptionhandler.MyException;
import com.edu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-17
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class VideoController {

	@Autowired
	private VideoService videoService;

	@Autowired
	private VodClient vodClient;

	//增加
	@PostMapping("/addVideo")
	public R addVideo(@RequestBody Video video){
		videoService.save(video);
		return R.ok();
	}

	//删除，
	//
	@DeleteMapping("/{id}")
	public R deleteVideo(@PathVariable String id){
		Video video = videoService.getById(id);
		String videoSourceId = video.getVideoSourceId();
		if (StringUtils.hasText(videoSourceId)){
			R result = vodClient.removeAlyVideo(videoSourceId);
			if (result.getCode() == 20001){
				throw new MyException(20001,"删除视频失败，熔断器是：");
			}
		}

		videoService.removeById(id);
		return R.ok();
	}

	/*修改，作业:
	* 1 先查
	* 2 在更新*/
	@GetMapping("/getVideo/{videoId}")
	public R getVideoById(@PathVariable String videoId){
		Video video = videoService.getById(videoId);
		return R.ok().data("video",video);
	}

	@PostMapping("/updateVideo")
	public R updateVideo(@RequestBody Video video){

		boolean b = videoService.updateById(video);
		return b==true ? R.ok():R.error();
	}

	//
}

